home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / lib / camera / camstream.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-08  |  7.5 KB  |  284 lines

  1. /* Copyright (c) 1992 The Geometry Center; University of Minnesota
  2.    1300 South Second Street;  Minneapolis, MN  55454, USA;
  3.    
  4. This file is part of geomview/OOGL. geomview/OOGL is free software;
  5. you can redistribute it and/or modify it only under the terms given in
  6. the file COPYING, which you should have received along with this file.
  7. This and other related software may be obtained via anonymous ftp from
  8. geom.umn.edu; email: software@geom.umn.edu. */
  9.  
  10. /* Authors: Charlie Gunn, Stuart Levy, Tamara Munzner, Mark Phillips */
  11.  
  12. #include <math.h>
  13. #include "handleP.h"
  14. #include "cameraP.h"
  15. #include "transobj.h"
  16.  
  17. HandleOps CamOps = {
  18.     "cam",
  19.     CamStreamIn,
  20.     CamStreamOut,
  21.     CamDelete,
  22.     NULL,        /* Resync */
  23.     NULL        /* close pool */
  24. };
  25.  
  26.  
  27. /*
  28.  * Load Camera from file.
  29.  * Syntax:
  30.  *  [camera] < "filename_containing_camera"    [or]
  31.  *  [camera] {   keyword  value   keyword  value   ...  }
  32.  *
  33.  */
  34. int
  35. CamStreamIn(Pool *p, Handle **hp, Camera **camp)
  36. {
  37.   char *w, *raww;
  38.   FILE *f;
  39.   Handle *h = NULL;
  40.   Handle *hname = NULL;
  41.   Camera *cam = NULL;
  42.   int credible = 0;
  43.   register int i,j;
  44.   float v;
  45.   int brack = 0;
  46.   int empty = 1, braces = 0;
  47.   static struct kw {
  48.     char *name;
  49.     char args;
  50.     int kbit;
  51.   } kw[] = {
  52.     { "camtoworld", 0, CAMF_NEWC2W },
  53.     { "worldtocam", 0, CAMF_W2C },
  54.     { "flag",        1, CAMF_PERSP|CAMF_STEREO },
  55.     { "halfyfield", 1, CAMF_FOV },
  56.     { "frameaspect",1, CAMF_ASPECT },
  57.     { "focus",        1, CAMF_FOCUS },
  58.     { "near",        1, CAMF_NEAR },
  59.     { "far",        1, CAMF_FAR },
  60.     { "stereo_sep", 1, CAMF_STEREOGEOM },
  61.     { "stereo_angle",1, CAMF_STEREOGEOM },
  62.     { "stereyes",   0, CAMF_STEREOXFORM },
  63.     { "whicheye",   1, CAMF_EYE },
  64.     { "define",        0, 0 },
  65.     { "camera",     0, 0 },
  66.     { "perspective",1, CAMF_PERSP },
  67.     { "stereo",     1, CAMF_STEREO },
  68.     { "hyperbolic", 1, 0 /* CAMF_HYPERBOLIC is now obsolete */ },
  69.     { "fov",        1, CAMF_FOV },
  70.   };
  71.   int got, bit;
  72.  
  73.   if((f = PoolInputFile(p)) == NULL)
  74.     return NULL;
  75.  
  76.   for(;;) {
  77.     switch(i = fnextc(f, 0)) {
  78.     case '<':
  79.     case ':':
  80.     case '@':
  81.     fgetc(f);
  82.     w = fdelimtok("(){}", f, 0);
  83.     if(i == '<' && HandleByName(w, &CamOps) == NULL) {
  84.         w = findfile(PoolName(p), raww = w);
  85.         if(w == NULL) {
  86.         OOGLSyntax(f, "Reading camera from \"%s\": can't find file \"%s\"",
  87.             PoolName(p), raww);
  88.         }
  89.     }
  90.     if((h = HandleReferringTo(i, w, &CamOps, hp)) != NULL) {
  91.         cam = (Camera *)h->object;
  92.     }
  93.     if(!brack) goto done;
  94.     break;
  95.  
  96.     case '{': brack++; fgetc(f); break;
  97.     case '}':
  98.     if(brack > 0) { fgetc(f); braces = 1; }
  99.     if(--brack <= 0) goto done;
  100.     /* Otherwise, fall through into... */
  101.     default:
  102.       empty = 0;
  103.       w = fdelimtok("(){}", f, 0);
  104.       if(w == NULL)
  105.     goto done;
  106.       
  107.       for(i = sizeof(kw)/sizeof(kw[0]); --i >= 0; )
  108.     if(!strcmp(w, kw[i].name))
  109.       break;
  110.  
  111.       if(i < 0) {
  112.     if(credible)
  113.         OOGLSyntax(f, "Reading camera from \"%s\": unknown camera keyword \"%s\"",
  114.             PoolName(p), w);
  115.     if(cam) CamDelete(cam);
  116.     return NULL;
  117.       } else if( (got=fgetnf(f, kw[i].args, &v, 0)) != kw[i].args ) {
  118.     OOGLSyntax(f, "Reading camera from \"%s\": \"%s\" expects %d values, got %d",
  119.           PoolName(p), w, kw[i].args, got);
  120.     return NULL;
  121.       }
  122.       if(i != 13 && cam == NULL) {
  123.     cam = CamCreate(CAM_END);
  124.     credible = 1;
  125.       }
  126.       if(cam) cam->changed |= kw[i].kbit;
  127.       switch(i) {
  128.       case 0:
  129.         if(TransStreamIn(p, &cam->c2whandle, cam->camtoworld))
  130.         CamSet(cam, CAM_C2W, cam->camtoworld,
  131.             CAM_C2WHANDLE, cam->c2whandle, CAM_END);
  132.         break;
  133.       case 1:
  134.         if(TransStreamIn(p, &cam->w2chandle, cam->worldtocam))
  135.         CamSet(cam, CAM_W2C, cam->worldtocam,
  136.             CAM_W2CHANDLE, cam->w2chandle, CAM_END);
  137.         break;
  138.       case 2: cam->flag = (int)v; break;
  139.       case 3: CamSet(cam, CAM_HALFYFIELD, v, CAM_END); break;
  140.       case 4: CamSet(cam, CAM_ASPECT, v, CAM_END); break;
  141.       case 5: CamSet(cam, CAM_FOCUS, v, CAM_END); break;
  142.       case 6: cam->near = v; break;
  143.       case 7: cam->far = v; break;
  144.       case 8: CamSet(cam, CAM_STEREOSEP, v, CAM_END); break;
  145.       case 9: CamSet(cam, CAM_STEREOANGLE, v, CAM_END); break;
  146.       case 10:
  147.         if(TransStreamIn(p, &cam->sterhandle[0], cam->stereyes[0]) &&
  148.            TransStreamIn(p, &cam->sterhandle[1], cam->stereyes[1]))
  149.         CamSet(cam, CAM_STEREYES, cam->stereyes,
  150.                 CAM_STERHANDLES, cam->sterhandle,
  151.                 CAM_END);
  152.         break;
  153.       case 11: cam->whicheye = (int)v; break;
  154.       case 12: /* "define" */
  155.         hname = HandleCreate( fdelimtok("(){}", f, 0), &CamOps );
  156.         break;
  157.       case 13: /* "camera" */ break;
  158.  
  159.       case 14: /* "perspective" */
  160.       case 15: /* "stereo" */
  161.     flagbit:
  162.         cam->flag &= ~kw[i].kbit;
  163.         if(v != 0) cam->flag |= kw[i].kbit;
  164.         break;
  165.  
  166.       case 16: /* "hyperbolic" */
  167.         /* The "hyperbolic" field is obsolete (replaced by the
  168.            "space" field.  Ignore it for now [ Mon Dec 7 14:05:50
  169.            1992 ].  After a few months [ say in March 1993 ] add a
  170.            warning message to be printed out at this point in the
  171.            code, and after a few more months [ say June 1993 ],
  172.            remove it completely. -- mbp */
  173.         break;
  174.  
  175.       case 17: /* "fov" */ CamSet(cam, CAM_FOV, v, CAM_END); break;
  176.     
  177.       }
  178.     }
  179.   }
  180.  
  181.  done:
  182.  
  183.   if(hname) {
  184.     if(cam)
  185.     HandleSetObject(hname, (Ref *)cam);
  186.     h = hname;
  187.   }
  188.   if(h == NULL && cam != NULL && p->ops == &CamOps) {
  189.     /*
  190.      * Dispose of nameless objects
  191.      */
  192.     h = HandleAssign(p->poolname, &CamOps, (Ref *)cam);
  193.   }
  194.  
  195.   /*
  196.    * Leave results where we're told to
  197.    */
  198.   if(h != NULL && hp != NULL && *hp != h) {
  199.     if(*hp)
  200.     HandlePDelete(hp);
  201.     *hp = h;
  202.   }
  203.   if(camp != NULL) {
  204.     RefIncr((Ref *)cam);
  205.     if(*camp != NULL)
  206.     CamDelete(*camp);
  207.     *camp = cam;
  208.   }
  209.   return (cam != NULL || h != NULL || (empty && braces));
  210. }
  211.  
  212.  
  213. int
  214. CamStreamOut(Pool *p, Handle *h, Camera *cam)
  215. {
  216.     int putdata;
  217.     float fov;
  218.     FILE *outf;
  219.  
  220.     if((outf = PoolOutputFile(p)) == NULL)
  221.     return 0;
  222.  
  223.     fprintf(outf, "camera {\n");
  224.  
  225.     if(cam == NULL && h != NULL && h->object != NULL)
  226.     cam = (Camera *)h->object;
  227.  
  228.     putdata = PoolStreamOutHandle( p, h, cam != NULL );
  229.     if(cam != NULL && putdata) {
  230.     if(cam->w2chandle) {
  231.         fprintf(outf, "worldtocam ");
  232.         TransStreamOut(p, cam->w2chandle, cam->worldtocam);
  233.     } else {
  234.         fprintf(outf, "camtoworld ");
  235.         TransStreamOut(p, cam->c2whandle, cam->camtoworld);
  236.     }
  237.     fprintf(outf, "\tperspective %d  stereo %d\n",
  238.         cam->flag & CAMF_PERSP ? 1 : 0,
  239.         cam->flag & CAMF_STEREO ? 1 : 0);
  240.     CamGet(cam, CAM_FOV, &fov);
  241.     fprintf(outf, "\tfov %g\n", fov);
  242.     fprintf(outf, "\tframeaspect %g\n", cam->frameaspect);
  243.     fprintf(outf, "\tfocus %g\n", cam->focus);
  244.     fprintf(outf, "\tnear %g\n", cam->near);
  245.     fprintf(outf, "\tfar %g\n", cam->far);
  246.     if(cam->flag & CAMF_STEREOGEOM) {
  247.         fprintf(outf, "\tstereo_sep %g\n", cam->stereo_sep);
  248.         fprintf(outf, "\tstereo_angle %g\n", cam->stereo_angle);
  249.     }
  250.     if(cam->flag & CAMF_EYE)
  251.         fprintf(outf, "\twhicheye %d\n", cam->whicheye);
  252.     if(cam->flag & CAMF_STEREOXFORM) {
  253.         fprintf(outf, "stereyes\n");
  254.         TransStreamOut(p, cam->sterhandle[0], cam->stereyes[0]);
  255.         fputc('\n', outf);
  256.         TransStreamOut(p, cam->sterhandle[1], cam->stereyes[1]);
  257.     }
  258.     }
  259.     fprintf(outf, "}\n");
  260.     return !ferror(outf);
  261. }
  262.  
  263. void
  264. CamHandleScan( Camera *cam, int (*func)(), void *arg )
  265. {
  266.     if(cam) {
  267.     if(cam->c2whandle)
  268.         (*func)(&cam->c2whandle, cam, arg);
  269.     if(cam->w2chandle)
  270.         (*func)(&cam->w2chandle, cam, arg);
  271.     }
  272. }
  273.  
  274. void
  275. CamTransUpdate( Handle **hp, Camera *cam, Transform T )
  276. {
  277.   TransUpdate( hp, (Ref *)cam, T );
  278.  
  279.   if(hp == &cam->c2whandle)
  280.     TmInvert( cam->camtoworld, cam->worldtocam );
  281.   else if(hp == &cam->w2chandle)
  282.     TmInvert( cam->worldtocam, cam->camtoworld );
  283. }
  284.